package com.swfee.qinben;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.active.ScheduleModel;
import com.swfee.qinben.dto.req.BindEquipementReqDto;
import com.swfee.qinben.dto.rsp.EquipmentRspDto;
import com.swfee.qinben.dto.rsp.ResultRspDto;
import com.swfee.qinben.httpactions.ScheduleAction;
import com.swfee.qinben.session.UserSession;
import com.swfee.qinben.util.CurenActivitUtils;

public class ScheduleOperActivity extends AppBaseActivity {

	private LayoutInflater inflater;

	Handler handle = new Handler();

	ScheduleModel scheduleModel;

	Spinner equiTypeId;
	Spinner equiCode;

	List<EquipmentRspDto> myEquis;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.schedule_opt_main);

		TextView bindAdvice = (TextView) findViewById(R.id.bind_advice);
		
		scheduleModel = getIntent().getParcelableExtra("scheduleModel");
		equiTypeId = (Spinner) findViewById(R.id.equi_type_id);
		equiCode = (Spinner)findViewById(R.id.equi_code);
		
		 final LinearLayout layNoDevice = (LinearLayout) findViewById(R.id.lay_no_device);
		 TextView tvNoDevice = (TextView) findViewById(R.id.tv_no_device_test);
		 
		tvNoDevice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (layNoDevice.getVisibility() == View.GONE) {
					layNoDevice.setVisibility(View.VISIBLE);
					CurenActivitUtils.setImageView(R.id.btn_arr, R.drawable.btn_arr_down);

				} else {
					layNoDevice.setVisibility(View.GONE);
					CurenActivitUtils.setImageView(R.id.btn_arr, R.drawable.btn_arr_left);
				}
			}
		});

		 Button submitResult = (Button) findViewById(R.id.submit_result);
		 submitResult.setOnClickListener(new OnClickListener() {
		
		 @Override
		 public void onClick(View v) {
			 Bundle bu = new Bundle();
			 bu.putParcelable("scheduleModel", scheduleModel);
			 CurenActivitUtils.startActivity(ScheduleOperSubmitActivity.class,false,bu);
		 }
		 }) ;
		 
		 

		bindAdvice.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				aysnDoBindDevice();
			}
		});

		//
		asynInitData();
	}

	//异常加载
	public void asynInitData() {

		showProgressDialog("加载信息数据，请稍候..");

		new Thread(new Runnable() {

			@Override
			public void run() {
				myEquis = new ScheduleAction().getEquipmentsByScheduleId(scheduleModel.getId() + "");
				final StringBuffer equiTypesNameStr = new StringBuffer();
				for (int i = 0; i < myEquis.size(); i++) {
					String str = myEquis.get(i).getEquipmentTypeName();
					if ((equiTypesNameStr.indexOf(str + ",") == -1)) {
						equiTypesNameStr.append(str);
						if (i != myEquis.size()) {
							equiTypesNameStr.append(",");
						}
					}

				}

				handle.post(new Runnable() {

					@Override
					public void run() {
						hideProgressDialog();
						String[] equTypes = null;
						if (equiTypesNameStr.length() > 0) {
							equTypes = equiTypesNameStr.toString().split(",");
						}
						CurenActivitUtils.setSpinnerByArray(R.id.equi_type_id,
								equTypes);
						final Spinner equiTypeId = (Spinner) findViewById(R.id.equi_type_id);
						equiTypeId.setOnItemSelectedListener(new OnItemSelectedListener() {

									@Override
									public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {

										String typeName = (String) equiTypeId.getSelectedItem();
										String ecodes = "";
										for (EquipmentRspDto dto : myEquis) {
											if (dto.getEquipmentTypeName().equals(typeName)) {
												ecodes += ","+ dto.getEquipmentSensorId();
											}

										}
										if (ecodes.startsWith(",")) {
											ecodes = ecodes.substring(1);
											CurenActivitUtils.setSpinnerByArray(R.id.equi_code,ecodes.split(","));
										}

									}

									@Override
									public void onNothingSelected(AdapterView<?> arg0) {

									}
								});

					}
				});

			}
		}).start();

	}

	public void aysnDoBindDevice() {
		showProgressDialog("绑定设备中，请稍候..");
		new Thread(new Runnable() {

			@Override
			public void run() {
				ScheduleAction action = new ScheduleAction();
				final BindEquipementReqDto reqDto = new BindEquipementReqDto();
				int selectEqId = getEquipmentIdByCode(equiCode.getSelectedItem().toString());
				reqDto.setEquipmentId(selectEqId);
				reqDto.setFamilyId(scheduleModel.getFamilyId());
				reqDto.setNurseName(UserSession.getName());
				reqDto.setScheduleId(scheduleModel.getId());
				final ResultRspDto resultDto = action.bindEquipementByScheduleId(reqDto);

				handle.post(new Runnable() {

					@Override
					public void run() {
						hideProgressDialog();
						if (resultDto.isSuccess()) {
							Bundle bu = new Bundle();
							bu.putParcelable("bindEquipementReqDto", reqDto);
							CurenActivitUtils.startActivity(ScheduleOperBindAdviceActivity.class,false,bu);
						} else {
							Toast.makeText(ScheduleOperActivity.this, "绑定失败",Toast.LENGTH_SHORT).show();
						}
					}
				});

			}
		}).start();

	}

	public Integer getEquipmentIdByCode(String code) {
		for (EquipmentRspDto dto : myEquis) {
			if (code.equals(dto.getEquipmentSensorId())) {
				return dto.getEquipmentId();
			}
		}
		return null;
	}
}